package wellness.activities;

import wellness.activities.beans.LoadBeans;
import wellness.activities.beans.TempoLivreBean;
import wellness.activities.data.TempoLivreData;
import wellness.activities.util.Dialogs;
import android.R.style;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.FrameLayout.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class FreeTime  extends Activity  {
	
	Context context;
	View tempoLivre;
	
	@Override
	protected void onRestart() {
		super.onRestart();
		popularTemposLivres();
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.freetime);
		context = this;
		
		ImageView goBack = (ImageView) findViewById(R.id.imageViewGoBack);
		goBack.setOnClickListener(new View.OnClickListener() {
            
        	public void onClick(View view) {
        		Intent intent = new Intent();
        		setResult(RESULT_OK, intent);
                finish();
            }
        });
        
		ImageView goHome = (ImageView) findViewById(R.id.imageViewGoHome);
        goHome.setOnClickListener(new View.OnClickListener() {
            
        	public void onClick(View view) {
        		Intent intent = new Intent(FreeTime.this, Main.class);
        		startActivity(intent);
            }
        });
        
        ImageView novo = (ImageView) findViewById(R.id.imageViewNewFreeTime);
        novo.setOnClickListener(new View.OnClickListener() {
            
        	public void onClick(View view) {
        		Intent intent = new Intent(FreeTime.this, NewFreeTime.class);
        		startActivity(intent);
            }
        });
        
        popularTemposLivres();
		
	}
	
	public void popularTemposLivres() {
		LinearLayout base = (LinearLayout) findViewById(R.id.linearLayoutBaseContent);
		base.removeAllViews();
		
		for (TempoLivreBean a : LoadBeans.getTemposLivres()) {

			FrameLayout base2 = new FrameLayout(context);
			LinearLayout base3 = new LinearLayout(context);
			base3.setOrientation(LinearLayout.VERTICAL);
			base3.setLayoutParams(new LayoutParams(
					LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT, Gravity.CENTER_VERTICAL));
			
			base2.setBackgroundResource(R.drawable.base_pequena);
			base2.setPadding(10, 10, 40, 10);

			TextView semana = new TextView(context);
			semana.setText(a.getSemana());
			semana.setPadding(20, 0, 0, 0);
			semana.setTextAppearance(this, style.TextAppearance_Medium);
			semana.setTextColor(Color.WHITE);
			semana.setLayoutParams(new LayoutParams(
					LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT,
					Gravity.CENTER_VERTICAL));
			
			TextView horario = new TextView(context);
			horario.setText("Das " + a.getDas() + " as " + a.getAs());
			horario.setPadding(20, 0, 0, 0);
			horario.setTextAppearance(this, style.TextAppearance_Small);
			horario.setTextColor(Color.WHITE);
			horario.setLayoutParams(new LayoutParams(
					LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT));

			ImageView remove = new ImageView(context);
			remove.setBackgroundResource(R.drawable.remover);
			remove.setId(a.getId());
			remove.setLayoutParams(new LayoutParams(30, 30,
					Gravity.CENTER_VERTICAL | Gravity.RIGHT));
			remove.setOnClickListener(new View.OnClickListener() {

				public void onClick(View view) {
					removerTempoLivre(view);
				}

			});

			base3.addView(semana);
			base3.addView(horario);
			base2.addView(base3);
			base2.addView(remove);

			base.addView(base2);

		}

	}

	public void removerTempoLivre(View v) {
		tempoLivre = v;

		DialogInterface.OnClickListener sim = new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int arg1) {
				TempoLivreData data = new TempoLivreData(context);
				if(data.delete(tempoLivre.getId()) > 0){
					LoadBeans.removerTempoLivrePorId(tempoLivre.getId());
					FrameLayout base2 = (FrameLayout) tempoLivre.getParent();
					LinearLayout base1 = (LinearLayout) base2.getParent();
					base1.removeView(base2);	
				}
				tempoLivre = null;
			}
		};

		Dialogs.showDialog("Aviso",
				"Deseja reamente remover esse tempo livre?", sim, null, context);

	}
	
}
